﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// Summary description for clsGioHang
/// </summary>
public class clsShoppingCart:DataTable
{

    public clsShoppingCart()
	{
        this.Columns.Add("idProduct",typeof(int)).DefaultValue = 0;
        this.Columns.Add("nameCategory", typeof(string)).DefaultValue = 0;
        this.Columns.Add("nameManufacturer", typeof(string)).DefaultValue = 0;
        this.Columns.Add("Model", typeof(string)).DefaultValue = 0;
        this.Columns.Add("UnitPrice", typeof(double)).DefaultValue = 0;
        this.Columns.Add("Quantity", typeof(double)).DefaultValue = 0;
        this.Columns.Add("Amount", typeof(double), "UnitPrice*Quantity");
        this.PrimaryKey = new DataColumn[] { this.Columns["idProduct"] };
	}

    public void Add(int idProduct, int Quantity)
    {
        this.AddProduct(idProduct, Quantity, false);
    }

    public void Update(int idProduct, int Quantity)
    {
        this.AddProduct(idProduct, Quantity, true);
    }

    public void Remove(int idProduct)
    {
        try
        {
            DataRow Product = this.Rows.Find(idProduct);
            this.Rows.Remove(Product);
        }
        catch
        {
            Console.WriteLine("Không tìm thấy sản phẩm !");
        }
    }

    public double Total
    {
        get
        {
            object value = this.Compute("SUM(Amount)", "");
            return value == DBNull.Value ? 0 : (double)value;
        }
    }

    public double Count
    {
        get
        {
            object value = this.Compute("SUM(Quantity)", "");
            return value == DBNull.Value ? 0 : (double)value;
        }
    }

    private void AddProduct(int idProduct, int Quantity, bool Update)
    {

        DataRow Item = this.Rows.Find(idProduct);
        if(Item != null)
        {
            Item["Quantity"] = Quantity + (Update ? 0 : (double)Item["Quantity"]);
        }
        else
        {
            String sql = "SELECT idProduct,nameCategory,nameManufacturer,Model,UnitPrice FROM Product,Category,Manufacturer WHERE idProduct=@idProduct and Category.idCategory = Product.idCategory and Product.idManufacturer = Manufacturer.idManufacturer";
           // Database.Fill(this, sql, "@idProduct", idProduct);
            Item = this.Rows.Find(idProduct);
            Item["Quantity"] = Quantity + (Update ? 0 : (double)Item["Quantity"]);
        }
    }
	
}
